package com.myschool.server.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Repository;

import com.myschool.server.dao.TimeTableDao;
import com.myschool.server.dao.support.BaseEntityDaoHibernate;
import com.myschool.server.model.TimeTableEntry;
import com.myschool.server.model.TimeTableType;

@Repository
@SuppressWarnings("unchecked")
public class TimetableDaoImpl extends BaseEntityDaoHibernate<TimeTableEntry>
		implements TimeTableDao {

	public TimetableDaoImpl() {
		super(TimeTableEntry.class);

	}

	public List<TimeTableEntry> retrieveTimeTableBySection(int sectionID) {
		List<TimeTableEntry> timeTable = getHibernateTemplate().find(
				" from TimeTableEntry timetable where timetable.sectionID = ?",
				sectionID);
		// TODO have to filter on usertype
		return timeTable;

	}

	@Override
	public List<TimeTableEntry> retrieveTimeTableByDate(Integer sectionId,
			Date startingFrom, short days) {

		Date endDate = DateUtils.addDays(startingFrom, days);
		List<TimeTableEntry> timeTable = getHibernateTemplate()
				.find(" from TimeTableEntry timetable where timetable.sectionID = ? and timetable.date between ? and ?",
						sectionId, startingFrom, endDate);
		return timeTable;
	}

	@Override
	public List<TimeTableEntry> retrieveTimeTableBySchoolSectionDate(
			Integer schoolId, Integer sectionID, Date startingFrom,
			TimeTableType type) {

		Date endDate = DateUtils.addDays(startingFrom, type.getDays());
		List<TimeTableEntry> timeTable = getHibernateTemplate()
				.find(" from TimeTableEntry timetable where timetable.sectionID = ? and timetable.date between ? and ?",
						sectionID, startingFrom, endDate);
		return timeTable;
	}

	
}
